﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace VipSys.Common.Library
{
    public class XMLBase
    {
        private XmlDocument m_xmlDoc;
        private string m_FilePath;
        public XmlDocument XmlDoc
        {
            get
            {
                return this.m_xmlDoc;
            }
            set
            {
                this.m_xmlDoc = value;
            }
        }

        private XmlNodeReader m_read;
        public XMLBase(string filePath)
        {
            this.m_FilePath = filePath;
            m_xmlDoc = new XmlDocument();
            try
            {
                m_xmlDoc.Load(filePath);
            }
            catch (Exception)
            {

                throw;
            }

        }

        /// <summary>
        /// 取节点集合
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public XmlNodeList SelectNodeList(string node)
        {
            return this.m_xmlDoc.SelectNodes(node);
        }
        /// <summary>
        /// 取节点
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public XmlNode SelectNode(string node)
        {
            return this.m_xmlDoc.SelectSingleNode(node);
        }

        /// <summary>
        /// 新增xml节点
        /// </summary>
        /// <param name="szName">节点名</param>
        /// <param name="szValue">节点值</param>
        /// <param name="xParentPath">父节点</param>
        public void AddNode(XmlElement node, string xParentPath)
        {
            XmlNode parentNode = this.m_xmlDoc.SelectSingleNode(xParentPath);
            parentNode.ParentNode.AppendChild(node);
            SaveXmlDocument();
        }

        public void DeleteNode(XmlNode clsDeletedNode)
        {
            clsDeletedNode.ParentNode.RemoveChild(clsDeletedNode);
        }
        /// <summary>
        /// 获取节点值
        /// </summary>
        /// <param name="read"></param>
        /// <returns></returns>
        public string ReadXmlNode(XmlNode xmlNode, string nodeName)
        {
            XmlNode node = xmlNode.SelectSingleNode(nodeName);
            this.m_read = new XmlNodeReader(node);
            string value = "";
            while (this.m_read.Read())
            {
                if (this.m_read.NodeType == System.Xml.XmlNodeType.Element)
                {
                    this.m_read.Read();
                    if (this.m_read.NodeType == System.Xml.XmlNodeType.Text)
                    {
                        value = this.m_read.Value;
                    }
                }
            }
            return value;
        }
        /// <summary>
        /// 将XML文档对象保存为XML文件
        /// </summary>
        public void SaveXmlDocument()
        {
            this.m_xmlDoc.Save(this.m_FilePath);
        }

    }
}
